package com.truyen.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;

public class DateTimeUtils {
	static Logger logger = Logger.getLogger(DateTimeUtils.class); 
	static final String DATEFORMAT = "dd/MM/yyyy";
	static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	static final SimpleDateFormat sdfZodiac = new SimpleDateFormat("dd/MM");//yyyy-MM-dd HH:mm:ss

	/**
	 * @return currrent date in UTC format
	 */
	public static Date GetUTCdatetimeAsDate() {
		// note: doesn't check for null
		return StringDateToDate(GetUTCdatetimeAsString());
	}

	/**
	 * @return UTC date time
	 */
	public static String GetUTCdatetimeAsString() {
		final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		final String utcTime = sdf.format(new Date());

		return utcTime;
	}
	
	public static Date formatDate(String dateformat, String source){
        try {
        	SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
            return dateFormat.parse(source);
        } catch (ParseException ex) {
            return null;
        }
    }
	
	public static String formatDate(String dateformat, Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
		return dateFormat.format(date);
	}

	/**
	 * @param StrDate
	 * @return convert to date from a string
	 */
	public static Date StringDateToDate(String StrDate) {
		Date dateToReturn = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

		try {
			dateToReturn = (Date) dateFormat.parse(StrDate);
		} catch (ParseException e) {
			logger.error("Error in StringDateToDate()",e);
		}

		return dateToReturn;
	}
	
	public static Date getDate(int nextDay){
		try {
			Calendar cld = Calendar.getInstance();
	        String strSource = String.format("%s, %s, %s", 
	                cld.get(Calendar.YEAR), 
	                cld.get(Calendar.MONTH) + 1, 
	                cld.get(Calendar.DATE) + nextDay);
			
			return sdf.parse(strSource);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	public static Date getDate(Date date, int nextDay){
		try {
			Calendar cld = Calendar.getInstance();
			cld.setTime(date);
	        String strSource = String.format("%s, %s, %s", 
	                cld.get(Calendar.YEAR), 
	                cld.get(Calendar.MONTH) + 1, 
	                cld.get(Calendar.DATE) + nextDay);
			
			return sdf.parse(strSource);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}

	
	//Zodiac
	/**
	 * @param source format("dd/MM")
	 * @return Date
	 */
	public static Date formatDate(String source){
        try {
            return sdfZodiac.parse(source);
        } catch (ParseException ex) {
            return null;
        }
    }
}
